package com.ss.android.article.base.feature.video;

import android.os.Bundle;
import android.os.Message;

import com.bytedance.common.utility.collection.WeakHandler;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;
import com.ss.ttvideoengine.b.VideoDataContainer;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class GetPlayUrlThread extends AbsApiThread {
    public static final String DATA_ERROR_1 = "data_error1";
    public static final String DATA_ERROR_2 = "data_error2";
    public static final String FIRST_ERROR = "error1";
    public static final String KEY_CODE = "code";
    public static final int MAX_GET_PLAY_URL_COUNT = 2;
    public static final int PLAYER_TYPE_CHAT = 3;
    public static final int PLAYER_TYPE_CLICK = 1;
    public static final int PLAYER_TYPE_LIVE = 2;
    public static final String SECOND_ERROR = "error2";
    public static final int STATUS_SUCCESS = 0;
    public static final String URL = "url";
    private long mAdId;
    private String mCategory;
    private String mDataError1;
    private String mDataError2;
    private String mError1;
    private String mError2;
    private WeakHandler mHandler;
    private volatile boolean mHasCancel;
    private long mItemId;
    private int mPlayType;
    private String mPlayUrl;
    private int mSp;
    private VideoDataContainer mVideoDataContainer;
    private String mVideoId;

    public GetPlayUrlThread(WeakHandler handler, int sp, String videoId, long itemId, String category, int playType, long adId, boolean imediate) {
        super("GetPlayUrlThread", imediate ? Priority.IMMEDIATE : Priority.HIGH);
        this.mPlayUrl = "";
        this.mHandler = handler;
        this.mVideoId = videoId;
        this.mSp = sp;
        this.mItemId = itemId;
        this.mCategory = category;
        this.mPlayType = playType;
        this.mAdId = adId;
    }

    public static boolean isApiSuccess(JSONObject arg1) throws JSONException {
        return arg1.getInt("code") == 0;
    }

    public void cancelRequest() {
        this.mHasCancel = true;
    }

    private String getBytedangcePlayUrl(String videoId) {
        HashMap v2 = new HashMap();
        v2.put("version", "1");
        v2.put("user", "toutiao");
        v2.put("video", videoId);
        v2.put("vtype", "mp4");
        String v3 = String.valueOf(new Date().getTime() / 1000);
        v2.put("ts", v3);
        TreeSet v0 = new TreeSet(v2.keySet());
        StringBuilder v4 = new StringBuilder();
        Iterator v5 = ((SortedSet) v0).iterator();
        while (v5.hasNext()) {
            Object v0_1 = v5.next();
            Object v1 = v2.get(v0_1);
            v4.append(((String) v0_1));
            v4.append(((String) v1));
        }

        v4.append("17601e2231500d8c3389dd5d6afd08de");
        String v0_2 = com.bytedance.common.utility.aa.md5(v4.toString());
        ArrayList v1_1 = new ArrayList();
        v1_1.add("1");
        v1_1.add("toutiao");
        v1_1.add(v3);
        v1_1.add(v0_2);
        v1_1.add("mp4");
        v1_1.add(videoId);
        v4.delete(0, v4.length());
        v4.append(ArticleConstants.videoPlayUrl);
        Iterator v1_2 = v1_1.iterator();
        while (v1_2.hasNext()) {
            v4.append(v1_2.next());
            v4.append("/");
        }

        return v4.substring(0, v4.length() - 1);
    }

    private String getLetvPlayUrl(String videoId) {
        Object v0_1;
        HashMap v2 = new HashMap();
        v2.put("user", "ff03bba36a");
        v2.put("video", videoId);
        v2.put("vtype", "mp4");
        v2.put("ts", String.valueOf(new Date().getTime() / 1000));
        TreeSet v0 = new TreeSet(v2.keySet());
        StringBuilder v3 = new StringBuilder();
        Iterator v4 = ((SortedSet) v0).iterator();
        while (v4.hasNext()) {
            v0_1 = v4.next();
            Object v1 = v2.get(v0_1);
            v3.append(((String) v0_1));
            v3.append(((String) v1));
        }

        v3.append("944fdf087f83a1f6b7aad88ec2793bbc");
        v2.put("sign", com.bytedance.common.utility.aa.md5(v3.toString()));
        Set v0_2 = v2.entrySet();
        v3.delete(0, v3.length());
        v3.append("http://api.letvcloud.com/getplayurl.php");
        v3.append("?");
        Iterator v2_1 = v0_2.iterator();
        while (v2_1.hasNext()) {
            v0_1 = v2_1.next();
            v3.append(((Map.Entry) v0_1).getKey());
            v3.append("=");
            v3.append(((Map.Entry) v0_1).getValue());
            v3.append("&");
        }

        return v3.substring(0, v3.length() - 1);
    }

    public void run() {
        JSONObject v4_1;
        long v6 = 0;
        int v0 = 11;
        super.run();
        int v3 = 0;
        label_5:
        while (v3 < 2) {
            try {
                switch (this.mSp) {
                    case 0:
                        this.mPlayUrl = this.getBytedangcePlayUrl(this.mVideoId);
                        if (!android.support.design.a.isTextEmpty(this.mPlayUrl)) {
                            StringBuilder v1_1 = new StringBuilder();
                            v1_1.append(this.mPlayUrl);
                            v1_1.append("?");
                            if (this.mPlayType > 0) {
                                v1_1.append("play_type");
                                v1_1.append("=");
                                v1_1.append(String.valueOf(this.mPlayType));
                            }

                            if (this.mItemId > v6) {
                                v1_1.append("&");
                                v1_1.append("item_id");
                                v1_1.append("=");
                                v1_1.append(String.valueOf(this.mItemId));
                            }

                            if (!android.support.design.a.isTextEmpty(this.mCategory)) {
                                v1_1.append("&");
                                v1_1.append("category");
                                v1_1.append("=");
                                v1_1.append(this.mCategory);
                            }

                            if (this.mAdId > v6) {
                                v1_1.append("&");
                                v1_1.append("ad_id");
                                v1_1.append("=");
                                v1_1.append(this.mAdId);
                            }

                            this.mPlayUrl = v1_1.toString();
                        } else {

                        }

                        break;
                    case 1:
                        this.mPlayUrl = this.getLetvPlayUrl(this.mVideoId);
                        break;
                }

                int v4 = 20480;
                String v5 = this.mPlayUrl;
                boolean v1_2 = this.mSp == 0;
                String v1_3 = NetworkUtils.doGet(v4, v5, v1_2);
                if (android.support.design.a.isTextEmpty(v1_3)) {
                    if (v3 == 0) {
                        this.mError1 = "empty response";
                    } else {
                        this.mError2 = "empty response";
                    }
                    ++v3;
                    continue;
                }

                v4_1 = new JSONObject(v1_3);
                if (!GetPlayUrlThread.isApiSuccess(v4_1)) {
                    if (v3 == 0) {
                        this.mError1 = "api fail";
                    } else {
                        this.mError2 = "api fail";
                    }
                    ++v3;
                    continue;
                }
                this.mVideoDataContainer = new VideoDataContainer();
            } catch (Exception v1) {
                if ((v1 instanceof SocketTimeoutException)) {
                    if (v3 == 0) {
                        this.mError1 = "time out";
                    } else {
                        this.mError2 = "time out";
                    }
                } else if ((v1 instanceof JSONException)) {
                    if (v3 == 0) {
                        this.mDataError1 = "data extract error";
                    } else {
                        this.mDataError2 = "data extract error";
                    }
                } else if (v3 == 0) {
                    this.mError1 = "net error";
                } else {
                    this.mError2 = "net error";
                }
                v1.printStackTrace();
                ++v3;
                continue;
            }

            try {
                this.mVideoDataContainer.initWithJO(v4_1);
                v0 = 10;
            } catch (Exception v1) {
                if (v3 != 0) {
                    this.mDataError2 = "data extract error";
                } else {
                    this.mDataError1 = "data extract error";
                }
                ++v3;
                continue;
            }

            if (!this.mHasCancel) {
                Message message = this.mHandler.obtainMessage(v0);
                Bundle bundle = new Bundle();
                bundle.putString("video_id", this.mVideoId);
                if (!android.support.design.a.isTextEmpty(this.mError1)) {
                    bundle.putString("error1", this.mError1);
                }

                if (!android.support.design.a.isTextEmpty(this.mError2)) {
                    bundle.putString("error2", this.mError2);
                }

                if (!android.support.design.a.isTextEmpty(this.mDataError1)) {
                    bundle.putString("data_error1", this.mDataError1);
                }

                if (!android.support.design.a.isTextEmpty(this.mDataError1)) {
                    bundle.putString("data_error2", this.mDataError2);
                }

                if (!android.support.design.a.isTextEmpty(this.mPlayUrl)) {
                    bundle.putString("url", this.mPlayUrl);
                }

                message.setData(bundle);
                message.obj = this.mVideoDataContainer;
                this.mHandler.sendMessage(message);
            }
            return;
        }
    }
}

